<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\ConsultingServer;
use app\common\model\Member;
use app\index\dao\ConsultingDao;
use app\index\model\UM as UM;
use app\index\traits\MessageSendTrait;

/**
 * Class ConsultingCancelService
 * @package app\index\service
 */
class ConsultingCancelService
{
    use MessageSendTrait;

    // dao
    private ConsultingDao $consultingDao;

    public function __construct(ConsultingDao $consultingDao)
    {
        $this->consultingDao = $consultingDao;
    }

    /**
     * 更新
     * @param UM $user
     * @param int $id
     * @param array $params
     */
    public function modify(UM $user, int $id, array $params): void
    {
        if ($user->type != '0') {
            return;
        }

        //这里如果要修改查询，建议把查询就放到这里
        $consulting = Consulting::field(true)->where('id', $id)->find();

        if (is_null($consulting)) {
            return;
        }

        $lawyerId = $consulting->lawyerId;

        if ($consulting->memberId != $user->id) {
            return;
        }

        if ($consulting->consultingType == 1) {
            return;
        }

        if ($consulting->matchType != 1) {
            return;
        }

        if (!is_id($consulting->lawyerId)) {
            return;
        }

        $success = $this->getConsultingLawyerMatchV2Service()->cancelDesignateLawyer($id);

        if ($success) {

            $lawyer = Member::where('id', $lawyerId)
                ->field(['id', 'nickname', 'realname', 'type'])
                ->append(['showName'])
                ->find();
            $showName = '';
            if ($lawyer) {
                $showName = $lawyer->showName;
            }
            $freeServer = ConsultingServer::where('id', $consulting->consultingServerId)->find();
            $this->sendLawyersAndRecommendServerMessage3($consulting, $freeServer->recommendServerId, $showName);

        }

    }

    private function getConsultingLawyerMatchV2Service(): ConsultingLawyerMatchV2Service
    {
        return app(ConsultingLawyerMatchV2Service::class);
    }

    private function getMessageSendService(): MessageSendService
    {
        return app(MessageSendService::class);
    }
}
